extends "res://base/base_lobby.gd"

var login_request : Dictionary = {}
var myself : Node2D

func _ready() -> void :
	super.request_ready()
	_init_peer_event()
	_custom_func()

func _init_peer_event() -> void :
	NetManage._client_authenticating.connect(_on_peer_authenticating)
	NetManage._server_connected.connect(_on_custom_server_connected)
	NetManage._client_authentication_failed.connect(_on_custom_authentication_failed)
	NetManage._create_multiplayer(multiplayer)

func _on_peer_authenticating(id: int) :
	multiplayer.send_auth(id,JSON.stringify(login_request).to_utf8_buffer())
	multiplayer.complete_auth(id)

#-----------------------------------------
@onready var canvas_layer: CanvasLayer = $CanvasLayer
@onready var user_name: LineEdit = $CanvasLayer/VBoxContainer/VBoxContainer/UserName
@onready var pass_word: LineEdit = $CanvasLayer/VBoxContainer/VBoxContainer/PassWord
@onready var commit: Button = $CanvasLayer/VBoxContainer/Commit

func _custom_func() -> void :
	commit.button_up.connect(_commit)
	$CanvasLayer2/Button.button_up.connect(func():
		rpc_id(1,"_change_room","Lobby2"))

func _commit() -> void :
	login_request.set("username",user_name.text)
	login_request.set("password",pass_word.text)
	multiplayer.multiplayer_peer = NetManage.peer
	commit.set_disabled(true)

func _on_custom_server_connected() -> void :
	canvas_layer.hide()

func _on_custom_authentication_failed(_id: int) -> void :
	commit.set_disabled(false)

@rpc("any_peer")
func _change_room(_room_name : String) : pass
